package com.project.schoolFoodShare.controller.list;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.config.utils.ShiroUtils;
import com.project.schoolFoodShare.domain.*;
import com.project.schoolFoodShare.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0
 * @description: 美食controller
 */
@Controller
@ResponseBody
@RequestMapping("list")
public class FoodListController {

    @Autowired
    private FoodListService foodListService;
    @Autowired
    private FoodRestaurantService foodRestaurantService;
    @Autowired
    private FoodCommentService foodCommentService;
    @Autowired
    private FoodSchoolService foodSchoolService;
    @Autowired
    private FoodFavorService foodFavorService;

    /** 分页获取美食 */
    @PostMapping("getFoodListPage")
    public Result getFoodListPage(@RequestBody FoodList foodList) {
        Page<FoodList> page = new Page<>(foodList.getPageNumber(),foodList.getPageSize());
        QueryWrapper<FoodList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(foodList.getUserId()),FoodList::getUserId,foodList.getUserId())
                .like(StringUtils.isNotBlank(foodList.getRestId()),FoodList::getRestId,foodList.getRestId())
                .eq(StringUtils.isNotBlank(foodList.getSchoolId()),FoodList::getSchoolId,foodList.getSchoolId())
                .like(StringUtils.isNotBlank(foodList.getName()),FoodList::getName,foodList.getName())
                .like(StringUtils.isNotBlank(foodList.getAddress()),FoodList::getAddress,foodList.getAddress())
                .like(StringUtils.isNotBlank(foodList.getOpen()),FoodList::getOpen,foodList.getOpen())
                .like(StringUtils.isNotBlank(foodList.getContent()),FoodList::getContent,foodList.getContent())
                .like(StringUtils.isNotBlank(foodList.getTypeId()),FoodList::getTypeId,foodList.getTypeId())
                .eq(foodList.getState() != null,FoodList::getState,foodList.getState())
                .orderByDesc(FoodList::getCreateTime);
        Page<FoodList> foodListPage = foodListService.page(page, queryWrapper);
        for (FoodList food : foodListPage.getRecords()) {
            List<String> list = Arrays.asList(food.getRestId().split(","));
            List<FoodRestaurant> foodRestaurants = foodRestaurantService.listByIds(list);
            List<String> name = new ArrayList<>();
            for (FoodRestaurant foodRestaurant : foodRestaurants) {
                name.add(foodRestaurant.getName());
            }
            food.setRestName(StringUtils.join(name, ","));
            FoodSchool foodSchool = foodSchoolService.getById(food.getSchoolId());
            food.setSchoolName(foodSchool.getName());
        }
        return Result.success(foodListPage);
    }

    @GetMapping("addLook")
    public Result addLook(@RequestParam("id")String id) {
        FoodList foodList = foodListService.getById(id);
        foodList.setLook(foodList.getLook() + 1);
        foodListService.updateById(foodList);
        return Result.success();
    }

    /** 根据id获取美食 */
    @GetMapping("getFoodListById")
    public Result getFoodListById(@RequestParam("id")String id) {
        FoodList foodList = foodListService.getById(id);
        List<String> list = Arrays.asList(foodList.getRestId().split(","));
        List<FoodRestaurant> foodRestaurants = foodRestaurantService.listByIds(list);
        List<String> name = new ArrayList<>();
        for (FoodRestaurant foodRestaurant : foodRestaurants) {
            name.add(foodRestaurant.getName());
        }
        foodList.setRestaurant(StringUtils.join(name, ","));
        return Result.success(foodList);
    }

    @GetMapping("getFoodListByFavor")
    public Result getFoodListByFavor(@RequestParam("school") String school) {
        QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodList::getSchoolId,school).orderByDesc(FoodList::getFavor).last("limit 3");
        List<FoodList> listList = foodListService.list(wrapper);
        return Result.success(listList);
    }

    @GetMapping("getFoodListByLook")
    public Result getFoodListByLook(@RequestParam("school") String school) {
        QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodList::getSchoolId,school).orderByDesc(FoodList::getLook).last("limit 8");
        List<FoodList> listList = foodListService.list(wrapper);
        return Result.success(listList);
    }

    @GetMapping("getFoodListByComment")
    public Result getFoodListByComment(@RequestParam("school") String school) {
        QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodList::getSchoolId,school).orderByDesc(FoodList::getComment).last("limit 3");
        List<FoodList> listList = foodListService.list(wrapper);
        return Result.success(listList);
    }

    @GetMapping("getFoodListByRestId")
    public Result getFoodListByRestId(@RequestParam("restId") String restId) {
        QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(FoodList::getRestId, restId).orderByDesc(FoodList::getCreateTime);
        List<FoodList> foodListList = foodListService.list(wrapper);
        return Result.success(foodListList);
    }

    /** 保存美食 */
    @PostMapping("saveFoodList")
    public Result saveFoodList(@RequestBody FoodList foodList) {
        User userInfo = ShiroUtils.getUserInfo();
        foodList.setUserId(userInfo.getId());
        boolean save = foodListService.save(foodList);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑美食 */
    @PostMapping("editFoodList")
    public Result editFoodList(@RequestBody FoodList foodList) {
        FoodList list = foodListService.getById(foodList.getId());
        if (list.getState() == 1) {
            foodList.setState(2);
        }
        boolean save = foodListService.updateById(foodList);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除美食 */
    @GetMapping("removeFoodList")
    @Transactional(rollbackFor = Exception.class)
    public Result removeFoodList(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                foodListService.removeById(id);
                QueryWrapper<FoodComment> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(FoodComment::getListId,id);
                foodCommentService.remove(wrapper);
                QueryWrapper<FoodFavor> wrapper1 = new QueryWrapper<>();
                wrapper1.lambda().eq(FoodFavor::getListId,id);
                foodFavorService.remove(wrapper1);
            }
            return Result.success();
        } else {
            return Result.fail("美食id不能为空！");
        }
    }

}
